package com.zryc.shop.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.cms.service.ISubjectService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 专题表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/cms/subject")
@Api(tags = "专题管理")
public class SubjectController {
    @Autowired
    private ISubjectService subjectService;
    @GetMapping("/listAll")
    @ApiOperation("查询所有专题")
    public ResponseResult listAll() {
        return new ResponseResult(200, "查询完成", subjectService.list());
    }

    @GetMapping("/list")
    @ApiOperation("分页查询专题列表")
    public ResponseResult list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               @RequestParam(required = false) String keyword) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "title", "create_time", "category_name", "category_id");
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.like("title", keyword);
        }
        subjectService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }
}
